{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "view-in-github" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 4: Data structure (Lists and Tuples)\n", "\n", "A data structure is a variable able to contain several values at the same time. A data structure can form a **sequence** if the values that compose it are arranged in a certain order. This is the case for **lists** and **tuples**. Conversely, a **Dictionary** does not form a sequence." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating Lists and Tuples\n", "A list or a tuple can contain any kind of value (int, float, bool, string). We say that they are heterogeneous structures.\n", "\n", "The difference between the 2 is that a list is **mutable** whereas a Tuple is not (you can't change it after it is created)\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Paris', 'Berlin', 'London', 'Brussels']\n" ] } ], "source": [ "# Lists\n", "list_1 = [1, 4, 2, 7, 35, 84]\n", "cities = ['Paris', 'Berlin', 'London', 'Brussels']\n", "nested_list = [list_1, cities] # a list can even contain lists! This is called a nested list\n", "\n", "#Tuples\n", "tuple_1 = (1, 2, 6, 2)\n", "\n", "print(cities)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. indexing and slicing\n", "In a sequence, each element is ordered according to an **index** (the first index being index 0)\n", "\n", "To access an element of a list or a tuple, we use a technique called **Indexing**.\n", "\n", "To access several elements of a list or a tuple, we use a technique called **Slicing**\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "sequence complete: ['Paris', 'Berlin', 'London', 'Brussels']\n", "index 0: Paris\n", "index 1: Berlin\n", "last index (-1): Brussels\n" ] } ], "source": [ "# INDEXING\n", "\n", "print('sequence complete:', cities)\n", "print('index 0:', cities[0])\n", "print('index 1:', cities[1])\n", "print('last index (-1):', cities[-1])\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "complete sequence: ['Paris', 'Berlin', 'London', 'Brussels']\n", "index 0-2: ['Paris', 'Berlin', 'London']\n", "index 1-2: ['Berlin', 'London']\n", "reverse order: ['Brussels', 'London', 'Berlin', 'Paris']\n" ] } ], "source": [ "# SLICING [start (included) : end (excluded) : step]\n", "\n", "print('complete sequence:', cities)\n", "print('index 0-2:', cities[0:3])\n", "print('index 1-2:', cities[1:3])\n", "print('reverse order:', cities[::-1])\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Useful actions on lists" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Paris', 'Berlin', 'London', 'Brussels']\n", "['Paris', 'Berlin', 'London', 'Brussels', 'Dublin']\n", "['Paris', 'Berlin', 'Madrid', 'London', 'Brussels', 'Dublin']\n", "['Paris', 'Berlin', 'Madrid', 'London', 'Brussels', 'Dublin', 'Amsterdam', 'Rome']\n", "length of the list: 8\n", "['Amsterdam', 'Berlin', 'Brussels', 'Dublin', 'London', 'Madrid', 'Paris', 'Rome']\n", "1\n" ] } ], "source": [ "cities = ['Paris', 'Berlin', 'London', 'Brussels'] # initial list\n", "print(cities)\n", "\n", "cities.append('Dublin') # Add an element at the end of the list\n", "print(cities)\n", "\n", "cities.insert(2, 'Madrid') # Add an element to the indicated index\n", "print(cities)\n", "\n", "cities.extend(['Amsterdam', 'Rome']) # Add a list at the end of our list\n", "print(cities)\n", "\n", "print('length of the list:', len(cities)) # display the length of the list\n", "\n", "cities.sort(reverse=False) # sort the list alphabetically / numerically\n", "print(cities)\n", "\n", "print(cities.count('Paris')) # count the number of times an element appears in the list\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lists and tuples work in harmony with the **if/else** and **For** control structures" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "yes\n", "Amsterdam\n", "Berlin\n", "Brussels\n", "Dublin\n", "London\n", "Madrid\n", "Paris\n", "Rome\n" ] } ], "source": [ "if 'Paris' in cities:\n", " print('yes')\n", "else:\n", " print('no')\n", "\n", "for element in cities:\n", " print(element)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The **enumerate** function is very useful to output both the elements of a list and their **index**. It's a very used function in datascience" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 Amsterdam\n", "1 Berlin\n", "2 Brussels\n", "3 Dublin\n", "4 London\n", "5 Madrid\n", "6 Paris\n", "7 Rome\n" ] } ], "source": [ "for index, element in enumerate(cities):\n", " print(index, element)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The **zip** function is also very useful to iterate through 2 lists in parallel. If one list is shorter than the other, the for loop stops at the shorter list" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Amsterdam 312\n", "Berlin 52\n", "Brussels 654\n", "Dublin 23\n", "London 65\n", "Madrid 12\n", "Paris 678\n" ] } ], "source": [ "list_2 = [312, 52, 654, 23, 65, 12, 678]\n", "for element_1, element_2 in zip(cities, list_2):\n", " print(element_1, element_2)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. exercise and Solution\n", "Transform the following code which gives the **Fibonacci sequence** to save the results in a list and return this list at the end of the function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise :" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def fibonacci(n):\n", " a = 0\n", " b = 1\n", " while b < n:\n", " a, b = b, a+b\n", " print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solution :" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "colab": {}, "colab_type": "code", "collapsed": true, "id": "DHS-JClnxsDx", "jupyter": { "outputs_hidden": true, "source_hidden": true }, "tags": [ "hide-cell" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987]\n" ] } ], "source": [ "def fibonacci(n):\n", " a = 0\n", " b = 1\n", " fib = [a] # Create a list fib \n", " while b < n:\n", " a, b = b, a+b\n", " fib.append(a) # append the new value of a to the end of fib\n", " return fib\n", "\n", "print(fibonacci(1000))" ] } ], "metadata": { "colab": { "authorship_tag": "ABX9TyNJAEomf56R9Aj2AZzGRpQF", "collapsed_sections": [], "include_colab_link": true, "name": "Untitled4.ipynb", "provenance": [] }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" } }, "nbformat": 4, "nbformat_minor": 4 }